package rs.mbs2.epics.server.session.dao.bean;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.Query;

import rs.mbs2.epics.server.entity.Photo;
import rs.mbs2.epics.server.entity.Tag;
import rs.mbs2.epics.server.session.dao.localinterface.PhotoDaoLocal;

@Stateless
@Local(PhotoDaoLocal.class)
@TransactionManagement(TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class PhotoDaoBean extends GenericDaoBean<Photo, Integer> implements PhotoDaoLocal {

	@SuppressWarnings("unchecked")
	@Override
	public List<Photo> getNPhotos(Integer n) {
		String query = "SELECT p FROM Photo p";
		Query q = em.createQuery(query);
		
		List<Photo> results = q.getResultList();
		List<Photo> retVal = new ArrayList<>();
		
		int i = 0;
		for(Photo p : results) {
			if(i < n)
				retVal.add(p);
			else
				break;
		}
		
		return retVal;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Photo> findPhotosByAlbumId(Integer albumId) {
		Query q = em.createNamedQuery("findPhotosByAlbumId");
		q.setParameter("albumId", albumId);

		return q.getResultList();
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<Photo> findPhotosByTitle(String title) {
		String query = "SELECT p FROM Photo p WHERE p.publiclyAvailable = true AND p.title LIKE '%" + title + "%'";
		Query q = em.createQuery(query);
		
		return q.getResultList();
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<Photo> findPhotosByDescription(String description) {
		String query = "SELECT p FROM Photo p WHERE p.publiclyAvailable = true AND p.description LIKE '%" + description + "%'";
		Query q = em.createQuery(query);
		
		return q.getResultList();
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<Photo> findPhotosByNameAndLastname(String firstname, String lastname) {
		/*
		 * SELECT p FROM Photo p LEFT JOIN p.albums a WHERE a.userThatCreatedAlbum.firstName LIKE :firstName"+
					" OR a.userThatCreatedAlbum.lastName LIKE :lastName
		*/
		
		String query = "SELECT p FROM Photo p LEFT JOIN p.albums a WHERE a.publiclyAvailable = true AND a.user.firstname LIKE '%" + firstname +
				"%' OR a.user.lastname LIKE '%" + lastname + "%'";
		Query q = em.createQuery(query);
		
		return q.getResultList();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Photo> findPhotosByTags(Tag tag) {
		String query = "SELECT tp FROM tagged_photos tp WHERE tp.tag_id = " + tag.getId();
		Query q = em.createQuery(query);
		
		return q.getResultList();
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<Photo> findPhotosByWidth(Integer width, Boolean greaterThanWidth) {
		String widthSign = "<=";
		if(greaterThanWidth)
			widthSign = ">=";
		
		String query = "SELECT p FROM Photo p WHERE p.publiclyAvailable = true AND p.width " + widthSign + " " + width;// + " " + widthSign + " p.width";
		Query q = em.createQuery(query);
		
		return q.getResultList();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Photo> findPhotosByHeight(Integer height, Boolean greaterThanHeight) {
		String heightSign = "<=";
		if(greaterThanHeight)
			heightSign = ">=";
		 
			String query = "SELECT p FROM Photo p WHERE p.publiclyAvailable = true AND p.height " + heightSign + " " + height;// + " " + heightSign + " p.height";
			Query q = em.createQuery(query);
			
			return q.getResultList();
	}

}
